<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="用户UID">
          <el-input v-model="form.uid" placeholder="用户UID" />
        </el-form-item>
        <el-form-item label="钻石数量排序">
          <el-select v-model="form.order_by" placeholder="请你选择">
            <el-option v-for="option in form.order_by_options" :key="option" :label="option" :value="option"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="APP标识">
          <el-select v-model="form.app_mark" placeholder="请选择条件">
            <el-option v-for="option in form.app_mark_options" :key="option" :label="option" :value="option" />
          </el-select>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker v-model="form.timeData" type="datetimerange" range-separator="至" start-placeholder="开始日期"
            end-placeholder="截止日期" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" width="100" />
          <el-table-column prop="uid" label="用户UID" align="center" />
          <el-table-column prop="total_diamond" label="钻石总数量" align="center" />
          <el-table-column prop="diamond" label="可用钻石" align="center" />
          <el-table-column prop="diamond_freeze" label="钻石冻结数量" align="center" />
          <el-table-column prop="gold" label="可用金币" align="center" />
          <el-table-column prop="gold_freeze" label="金币冻结数量" align="center" />
          <el-table-column prop="app_mark" label="APP标识" align="center" />
          <el-table-column prop="createtime" label="创建时间" align="center" :formatter="formatterTime" width="180" />
        </el-table>
        <div class="table-pagination">
          <el-pagination :current-page="pageable.pageNum" :page-size="pageable.pageSize" :page-sizes="[10, 25, 50, 100]"
            :background="true" layout="total, sizes, prev, pager, next, jumper" :total="pageable.total"
            @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive,computed } from "vue";

import { formatterTime, exportToExcel,timestamp } from "@/utils/utils";
import { assetsListApi } from "@/api/slotsgame/assets/assets";

const tableData = ref([]);
//钻石数从大到小倒序值: 'diamond desc',从小到大正序值: 'diamond asc'
const formData = {
  uid: "",
  app_mark: "",
  app_mark_options: ["cash", "norm",],
  order_by_options:["从大到小","从小到大"],
  timeData: [],
};


const form = reactive({ ...formData });
const loading = ref(false);
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0
});

const order_byValue = computed(() => {
  if (form.order_by === "从大到小") {
    return "diamond desc";
  } else if (form.order_by === "从小到大") {
    return "diamond asc";
  } else {
    return "";
  }
});
// 参数
// const param = () => {
//   return {
//     page: pageable.pageNum,
//     pagesize: pageable.pageSize,
//     uid: form.uid
//   };
// };
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    uid: form.uid,
    app_mark: form.app_mark,
    order_by: order_byValue.value,
    // timeData: form.timeData,
    start_time: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end_time: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : ""
  };
};


// 获取表格数据（优化）
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await assetsListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};




// const getTbaleData = async () => {
//   loading.value = true;

//   try {
//     const res = await assetsListApi(param());
//     loading.value = false;
//     tableData.value = res.data.results;
//     pageable.total = res.data.count;
//   } catch (error) {
//     loading.value = false;
//   }
// };


// 重置
const onReset = () => {
  Object.assign(form, formData);
  form.order_by = "";
  getTbaleData();
  pageable.pageNum = 1;
};

// const onReset = () => {
//   Object.assign(form, formData);
// };

// 查询
const onQuery = async () => {
  getTbaleData();
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await assetsListApi(data);

  let head = {
    id: "ID",
    uid: "用户UID",
    total_diamond: "	钻石总数量",
    diamond: "可用钻石数量",
    diamond_freeze: "冻结钻石数量",
    app_mark: "APP标识",
    createtime: "创建时间"
  };
  exportToExcel(res.data.results, head, "Slotsgame-钻石资产");
};

// 条数
const handleSizeChange = number => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = number => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
